#include "GuiManager.h"
#include "GuiWidget.h"
#include "GuiContainer.h"
#include "FontManager.h"

#include <GL/gl.h>
#include <GL/glu.h>
#include <list>
#include <iostream>
#ifdef DEBUG_NEW
#include "debug/debug_new.h"
#endif

using namespace std;

GuiManager::GuiManager(int width, int height, FontManager* font) {
    _font = font;
    _root = new GuiContainer(new GuiRect(0, 0, width, height), 0);
    _root->prop()->zindex = -1000.0f;
}

GuiManager::~GuiManager() {
    delete _root;
}

void GuiManager::add_widget(GuiWidget* w) {
    _root->add_child(w);
}
void GuiManager::input(SDL_Event* evt, unsigned int ms) {
    _root->input(evt, ms);
}
void GuiManager::update(unsigned int ms) {
    _root->update(ms);
}

void GuiManager::draw() {
    glPushMatrix();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    int* v = _root->rect()->value();

    //std::cout << v[GUI_W] << " " << v[GUI_H] << "\n";
    glOrtho(0, v[GUI_W], v[GUI_H], 0, -100, 100);
    _root->draw(_font);
//    _draw(_root);
    //ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
//    string str = "Yo Man, what's\nup dog. I am\nan email address at\nyo@whatup.awesome bro\n";
//    float f1, f2;
//    glColor3f(1, 0, 0);
//    _font->render(str, 0, 0, 100, 600, 1, GUI_ALIGN_LEFT, GUI_VALIGN_TOP, &f1, &f2);
//    glColor3f(0, 1, 0);
//    _font->render(str, 0, 0, 100, 600, 1, GUI_ALIGN_CENTER, GUI_VALIGN_TOP, &f1, &f2);
//    glColor3f(0, 0, 1);
//    _font->render(str, 0, 0, 100, 600, 1, GUI_ALIGN_RIGHT, GUI_VALIGN_TOP, &f1, &f2);
//    glColor3f(1, 1, 0);
//    _font->render(str, 0, 0, 100, 600, 1, GUI_ALIGN_LEFT, GUI_VALIGN_CENTER, &f1, &f2);
//    glColor3f(1, 0, 1);
//    _font->render(str, 0, 0, 100, 600, 1, GUI_ALIGN_CENTER, GUI_VALIGN_CENTER, &f1, &f2);
//    glColor3f(0, 1, 1);
//    _font->render(str, 0, 0, 100, 600, 1, GUI_ALIGN_RIGHT, GUI_VALIGN_CENTER, &f1, &f2);
//    glColor3f(0.5, 0.5, 0.5);
//    _font->render(str, 0, 0, 100, 600, 1, GUI_ALIGN_LEFT, GUI_VALIGN_BOTTOM, &f1, &f2);
//    glColor3f(1, 1, 1);
//    _font->render(str, 0, 0, 100, 600, 1, GUI_ALIGN_CENTER, GUI_VALIGN_BOTTOM, &f1, &f2);
//    glColor3f(0.75, 0.75, 0.75);
//    _font->render(str, 0, 0, 100, 600, 1, GUI_ALIGN_RIGHT, GUI_VALIGN_BOTTOM, &f1, &f2);
//    std::cout << f1 << ", " << f2 << "\n";
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
}

//void GuiManager::_draw(GuiWidget* w) {
//    if (w->bg_display() == 0) {
//        _build_bg_display(w);
//    }
//
//    glPushMatrix();
//    if (w->bg_texture()) glBindTexture(GL_TEXTURE_2D, w->bg_texture());
//    glCallList(w->bg_display());
//    glPopMatrix();
//
//    GuiContainer* c = dynamic_cast<GuiContainer*>(w);
//    if (c) {
//        list<GuiWidget*>* ch = c->get_children();
//        for (list<GuiWidget*>::iterator it = ch->begin();
//                it != ch->end(); ++it) {
//            _draw(*it);
//        }
//    }
//    else {
//        if (w->display() == 0) {
//            _build_display(w);
//        }
//        glPushMatrix();
//        if (w->texture()) glBindTexture(GL_TEXTURE_2D, w->texture());
//        glCallList(w->display());
//        glPopMatrix();
//    }
//}

